Page({
  data: {
    showContent:false,
    showNoResult:false,
    clickedId: -1,
    voices: []
  },
  onLoad(options) {

  },
  searchVoice(event) {
    var that = this
    console.log(event.detail.value.ch)
    wx.cloud.callFunction({
      name: 'fuzzySearch',
      data: {
        inputValue: event.detail.value.ch
      }
    }).then(res => {
      if(res.result.data.length > 0){
        that.setData({
          showContent: true,
          showNoResult: false,
          voices: res.result.data
        })
      }else {
        that.setData({
          showNoResult: true,
          showContent: false
        })
      }
    }).catch(err => {
      that.setData({
        showContent: false,
        showNoResult: true,
        voices: []
      })
    })
  },
  speak(event) {
    var that = this
    console.log(event.currentTarget.dataset)
    this.setData({
      clickedId: event.currentTarget.dataset.index,
      showPressed: true
    })
    let voiceUrl = event.currentTarget.dataset.url
    const innerAudioContext = wx.createInnerAudioContext({
      useWebAudioImplement: true
    })
    innerAudioContext.autoplay = true
    innerAudioContext.src = voiceUrl

    innerAudioContext.onEnded(() => {
      console.log("结束播放")
      that.setData({
        showPressed: false,
        clickedId: -1
      })
    })
    innerAudioContext.onError((res) => {
      console.log(res.errCode)
      if(res.errCode == 10001){
        wx.showToast({
          title: '系统错误',
          icon: "error"
        })
      }else if(res.errCode == 10002){
        wx.showToast({
          title: '网络错误',
          icon: "error"
        })
      }else if(res.errCode == 10003){
        wx.showToast({
          title: '文件错误',
          icon: "error"
        })
      }else if(res.errCode == 10004){
        wx.showToast({
          title: '格式错误',
          icon: "error"
        })
      }else{
        wx.showToast({
          title: '未知错误',
          icon: "error"
        })
      }
    })
    
    // innerAudioContext.play()
  }
})